package com.fametal.ui.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

public class Utils {
	
	private static String result;
	
	public static void setActivityContent(ActivityGroup activity, String id, ViewGroup container, Class className) {
		Window window = activity.getLocalActivityManager().startActivity(
                id,
                new Intent(activity.getApplicationContext(), className).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		container.removeAllViews();
		View contentView = window.getDecorView();
		container.addView(contentView);
	}

	public static void post(String url, List<NameValuePair> params){
		HttpPost httpPost = new HttpPost(url); 
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
	}
	
	public static void get(final Activity activity, final String url, final boolean back) {
		ThreadPool.excuteLongTask(new Runnable() {
			@Override
			public void run() {
				HttpClient httpClient = new DefaultHttpClient();
				HttpGet httpRequest = new HttpGet(url);
				HttpResponse httpResponse;
				try {
					httpResponse = httpClient.execute(httpRequest);
					if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
						result = EntityUtils.toString(httpResponse.getEntity());
						if(back){
							mFinishedListener.finished(result);
						}
					}else {
						result = "请求失败";
					}
					activity.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							DialogUtils.info(activity, "请求返回值", result, null);
						}
					});
				} catch (final Exception e) {
					activity.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							DialogUtils.info(activity, e.getMessage(), result, null);
						}
					});
				} 
			}
		});
	}
	
	public static boolean isConnected(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(connectivity != null) {
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if(info != null && info.isConnected()){
				if(info.getState() == NetworkInfo.State.CONNECTED){
					return true;
				}
			}
		}
		return false;
	}
	
	private static GetFinishedListener mFinishedListener;
	
	public static void setGetFinishedListener(GetFinishedListener listener){
		mFinishedListener = listener;
	}
	
	public interface GetFinishedListener {
		public void finished(String result);
	}
}
